package com.chukun.spring.dependency.injection;

import com.chukun.spring.dependency.injection.annotation.UserGroup;
import com.chunkun.ioc.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Collection;

/**
 * {@link Qualifier} 使用Qualifier注解，实现对象的分组操作
 * @author chukun
 *
 */
public class QualifierAnnotationDependencyInjectionDemo {

    @Autowired
    private User user; // superUser -> primary =true

    @Autowired
    @Qualifier("user") // 指定 Bean 名称或 ID
    private User namedUser;

    @Autowired
    /**
     * 不包括 Qualifier 注解的bean
     */
    private Collection<User> allUsers;

    @Autowired
    @Qualifier
    /**
     * 只包括 Qualifier 注解的bean
     */
    private Collection<User> qualifiedUsers;

    @Autowired
    @UserGroup
    /**
     * 只包括 UserGroup 注解的bean
     */
    private Collection<User> groupUsers;

    @Bean
    @Qualifier
    public User user01() {
        return createUser(05L);
    }

    @Bean
    @Qualifier
    public User user02() {
        return createUser(06L);
    }

    @Bean
    @UserGroup
    public User user03() {
        return createUser(10L);
    }

    @Bean
    @UserGroup
    public User user04() {
        return createUser(11L);
    }

    private static User createUser(Long id) {
        User user = new User();
        user.setId(id);
        return user;
    }

    public static void main(String[] args) {

        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class（配置类） -> Spring Bean
        applicationContext.register(QualifierAnnotationDependencyInjectionDemo.class);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        // 加载 XML 资源，解析并且生成 BeanDefinition
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        // 依赖查找 QualifierAnnotationDependencyInjectionDemo Bean
        QualifierAnnotationDependencyInjectionDemo demo = applicationContext.getBean(QualifierAnnotationDependencyInjectionDemo.class);

        // 期待输出 superUser Bean
        System.out.println("demo.user = " + demo.user);
        // 期待输出 user Bean
        System.out.println("demo.namedUser = " + demo.namedUser);
        // 期待输出 superUser user
        System.out.println("demo.allUsers = " + demo.allUsers);
        // 期待输出 user01 user02
        System.out.println("demo.qualifiedUsers = " + demo.qualifiedUsers);
        // 期待输出 user03 user04
        System.out.println("demo.groupedUsers = " + demo.groupUsers);


        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }

}
